﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;


namespace GSLMS.WebAPP.Areas.System.Controllers.Organize
{
    public class OrganizeController : AdminBaseController
    {
        protected override string navTabId
        {
            get { return "System_Organize"; }
        }

        //
        // GET: /System/Organize/

        public ActionResult Index()
        {
            SearchPara(null);
            InitSelectList();
            return View(GetList(null));
        }

        //
        // GET: /System/Organize/Details/5

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            SearchPara(form);
            InitSelectList();
            return View(GetList(form));
        }

        //
        // GET: /System/Organize/Create

        public ActionResult Create(long? id)
        {
            TB_SYS_ORG model = null;
            if (id > 0)
            {
                model = serviceSysOrg.GetByID((long)id);
                model.MODIFYDATE = DateTime.Now;
                model.MODIFYBY = "louchengbang";
            }
            else
            {
                model = new TB_SYS_ORG();
                model.CREATEBY = "louchengbang";
                model.CREATEDATE = DateTime.Now;
                model.MODIFYDATE = DateTime.Now;
                model.MODIFYBY = "louchengbang";
            }
            if(id>0)
              ViewData["orgList"] = new SelectList(serviceSysOrg.GetAllList((long)id), "id", "orgname");
            else
                ViewData["orgList"] = new SelectList(serviceSysOrg.GetAllList(), "id", "orgname");
            ViewData["empList"] = new SelectList(serviceEmp.GetEffAllEmp(), "id", "empname");
            return View(model);
        }

        //
        // POST: /System/Organize/Create

        [HttpPost]
        public string Create(FormCollection collection)
        {
            string result = string.Empty;
            TB_SYS_ORG model = new TB_SYS_ORG();

            TryUpdateModel(model, collection);

            bool flag = model.ID > 0 ? serviceSysOrg.Update(model) : serviceSysOrg.Add(model);
            if (flag)
            {
                result = AjaxResultSucess;
            }
            else
            { result = AjaxResultFail; }

            return "{" + result + "}";
        }


        // POST: /System/Organize/Delete/5

        [HttpPost]
        public string Delete(long id, FormCollection collection)
        {
            string result = string.Empty;
            try
            {
                bool isSumbit = serviceSysOrg.Delete(id);
                if (isSumbit)
                {
                    result = AjaxDeleteSucess;
                }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxDeleteFail;
            }

            return "{" + result + "}";
        }

        private List<TB_SYS_ORG> GetList(FormCollection form)
        {
            var list = new List<TB_SYS_ORG>();
            var searchParma = new TB_SYS_ORG();
            if (ViewData["SearchParma"] == null)
            {
                if (form == null || form.Count == 0)
                {
                    searchParma.ISSITE = -1;
                    
                }
                else
                {
                    TryUpdateModel(searchParma, form);
                }
            }
            else
                searchParma = ViewData["SearchParma"] as GSLMS.Entity.TB_SYS_ORG ?? new GSLMS.Entity.TB_SYS_ORG();

            list = serviceSysOrg.GetList(searchParma);
            list = list ?? new List<TB_SYS_ORG>();
            ViewData["RowCount"] = searchParma.RowCount;
            return list;
        }

        private TB_SYS_ORG SearchPara(FormCollection form)
        {
            var searchParma = new TB_SYS_ORG();
            if (form != null && form.Count > 0)
            {
                TryUpdateModel(searchParma, form);
            }
            ViewData["SearchParma"] = searchParma ?? new TB_SYS_ORG();
            return searchParma;
        }

        private void InitSelectList()
        {
            ViewData["EmpList"] = serviceEmp.GetUserAllList();
            ViewData["ISSITELIST"] = EnumHelper.ToSelectList<SystemEnum.ISSITE>();
        }
    }
}
